import 'package:flutter/material.dart';
import '../demo/ui_demo.dart';
import '../local/pages/context_header.dart';
class MyDemo extends StatelessWidget implements Demo {
  MyDemo(this.title);
  @override
  String title;
  @override
  Widget build(BuildContext context) {
    return new HomePage(title: this.title,);
  }
}

class HomePage extends StatelessWidget {
  String title;
  HomePage({this.title});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
        centerTitle: true,
        elevation: 0,
        actions: <Widget>[
          IconSource("Dialog","11"),
        ],
      ),
      body: _DialogDemo(),
    );
  }
}

class ListItem extends StatelessWidget {
  final Widget widget;
  final String title;
  const ListItem(this.title, this.widget);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ListTile(
      title: Text(this.title),
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
          return widget;
        }));
      },
    );
  }
}

class _DialogDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ListView(
      children: <Widget>[
        ListItem("SimpleDialog", SimpleDialogDemo()),
        ListItem("AlertDialog", ALertDialogDemo()),
        ListItem("BottomSheet", BottomSheetDemo()),
        ListItem("SnackBar", SnackBarDemo()),
        ListItem("ExpansionPanel", ExpansionPanelDemo()),
      ],
    );
  }
}

class SimpleDialogDemo extends StatefulWidget {
  @override
  _SimpleDialogDemoState createState() => _SimpleDialogDemoState();
}

enum Action { A, B, C }

class _SimpleDialogDemoState extends State<SimpleDialogDemo> {
  String _choice = "nothing";
  _showSimpleDialog() async {
    final action = await showDialog(
        //barrierDismissible:false,
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            title: Text("your choice?"),
            children: <Widget>[
              SimpleDialogOption(
                child: Text("option A"),
                onPressed: () {
                  App.pop(context, Action.A);
                },
              ),
              SimpleDialogOption(
                child: Text("option B"),
                onPressed: () {
                  Navigator.pop(context, Action.B);
                },
              ),
              SimpleDialogOption(
                child: Text("option C"),
                onPressed: () {
                  Navigator.pop(context, Action.C);
                },
              ),
            ],
          );
        });
    switch (action) {
      case Action.A:
        setState(() {
          _choice = "A";
        });
        break;
      case Action.B:
        setState(() {
          _choice = "B";
        });
        break;
      case Action.C:
        setState(() {
          _choice = "C";
        });
        break;
      default:
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("SimpleDialog")),
      body: Container(
        alignment: Alignment(0, 0),
        child: Text("your choice is: $_choice"),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.question_answer),
        onPressed: _showSimpleDialog,
      ),
    );
  }
}

class ALertDialogDemo extends StatefulWidget {
  @override
  _ALertDialogDemoState createState() => _ALertDialogDemoState();
}

class _ALertDialogDemoState extends State<ALertDialogDemo> {
  String _choice = "nothing";
  _openAlertDialog() async {
    var action = showWarningAlertDialog(context,"确定吗？");
    switch (action) {
      case "OK":
        setState(() {
          _choice = action;
        });
        break;
      case "CANCLE":
        setState(() {
          _choice = action;
        });
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("AlertDialog"),
      ),
      body: Container(
        alignment: Alignment(0, 0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("your choice is：$_choice"),
            OutlineButton(
              child: Text("Open AlertDialog"),
              onPressed: _openAlertDialog,
            )
          ],
        ),
      ),
    );
  }
}

class BottomSheetDemo extends StatefulWidget {
  @override
  _BottomSheetDemoState createState() => _BottomSheetDemoState();
}

class _BottomSheetDemoState extends State<BottomSheetDemo> {
  final _bottomSheetScaffoldKey = GlobalKey<ScaffoldState>();
  String _choice="nothing";
  _openBottomSheet() {
    _bottomSheetScaffoldKey
        .currentState
        .showBottomSheet((BuildContext context){
          return BottomAppBar(
            child: Container(
              padding: EdgeInsets.only(left: 10.0),
              height: 90,
              child: Row(
                children: <Widget>[
                  Icon(Icons.info),
                  SizedBox(width: 30.0,),
                  Text("BottomAppBar"),
                  Expanded(
                    child: Text("do you have any other tip ?",textAlign: TextAlign.right,),
                  )
                ],
              ),
            ),
          );
    });
  }

 _openModelBottomSheet() async{
   final option= await showModalBottomSheet(context: context, builder: (BuildContext context){
      return Container(
        height: 280,
        padding: EdgeInsets.all(10.0),
        child: Column(
          children: <Widget>[
            ListTile(
              title: Text("option A"),
              onTap: (){
                Navigator.pop(context,"A");
              },
            ),
            ListTile(
              title: Text("option B"),
              onTap: (){
                Navigator.pop(context,"B");
              },
            ),
            ListTile(
              title: Text("option C"),
              onTap: (){
                Navigator.pop(context,"C");
              },
            ),
          ],
        ),
      );
    });
   setState(() {
     _choice=option;
   });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _bottomSheetScaffoldKey,
      appBar: AppBar(
        title: Text("BottomSheet"),
      ),
      body: Container(
        alignment: Alignment(0, 0),
        child:Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            OutlineButton(
              child: Text("Open BottomSheet"),
              onPressed: _openBottomSheet,
            ),
            OutlineButton(
              child: Text("Open Model BottomSheet"),
              onPressed: _openModelBottomSheet,
            ),
            Text("your choice is:$_choice"),
          ],
        ),
      ),
    );
  }
}

class SnackBarDemo extends StatefulWidget {
  @override
  _SnackBarDemoState createState() => _SnackBarDemoState();
}

class _SnackBarDemoState extends State<SnackBarDemo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("SnackBar"),),
      body: Container(
        alignment: Alignment(0, 0),
        child:ShowSnackButton() ,
      ),
    );
  }
}
class ShowSnackButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return OutlineButton(
      child: Text("Open SnackBar"),
      onPressed: (){
        Scaffold.of(context).showSnackBar(SnackBar(
          duration: Duration(seconds: 1),
          content: Text("this is a SnackBar"),
          action: SnackBarAction(
            label: "OK",
            onPressed: (){
              //Navigator.of(context).pop();
            },
          ),
        ));
      },
    );
  }
}

class ExpansionPanelDemo extends StatefulWidget {
  @override
  _ExpansionPanelDemoState createState() => _ExpansionPanelDemoState();
}

class _ExpansionPanelDemoState extends State<ExpansionPanelDemo> {
List<ExpansionPanelItem> _expanseonPanels=[
  ExpansionPanelItem(
    isExpanded: false,
    headerText: "header text A",
    widget: Container(
      padding: EdgeInsets.all(8.0),
      child: Text("This is  content A"),
    )
  ),
  ExpansionPanelItem(
    isExpanded: false,
    headerText: "header text B",
    widget: Container(
      padding: EdgeInsets.all(8.0),
      child: Text("This is  content B"),
    )
  ),
  ExpansionPanelItem(
    isExpanded: false,
    headerText: "header text C",
    widget: Container(
      padding: EdgeInsets.all(8.0),
      child: Text("This is  content C"),
    )
  ),
];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("ExpansionPanel"),),
      body: Container(
       padding: EdgeInsets.all(8.0),
        child: Column(
          children: <Widget>[
            ExpansionPanelList(
              expansionCallback: (int index,bool isExpand){
                setState(() {
                  _expanseonPanels[index].isExpanded=!isExpand;
                });
              },
              children: _expanseonPanels.map((ExpansionPanelItem item){
                return ExpansionPanel(
                    isExpanded: item.isExpanded,
                    headerBuilder: (BuildContext context, bool isExpanded){
                      return Container(
                        child: Container(
                          padding: EdgeInsets.all(10.0),
                          child: Text(item.headerText),
                        ),
                      );
                    },
                    body: item.widget
                );
              }).toList(),
            )
          ],
        ),
      ),
    );
  }
}

class ExpansionPanelItem {
  final Widget widget;
  final String headerText;
  bool isExpanded;
  ExpansionPanelItem({
    this.widget,this.headerText,this.isExpanded
});

}



